package network;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ConnectException;
import java.net.Socket;

import comp.Comp;

public class NetworkMain implements Serializable {
	private Socket socket;
	private String serverIP;
	private int serverPort;
	private String ID;
	private Thread receiver;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Comp comp = new Comp();
	private boolean exitflag = false;

	public NetworkMain() {
		socket = null;
		serverIP = null;
	}

	public class ReceiveMessage implements Runnable {
		public void run() {
			Protocol p = new Protocol();
			Object O = null;
			try {
				while ((O = in.readObject()) != null) {
					p = (Protocol) O;
					switch (p.getCmd()) {
					case Protocol.Prtlogin: {
						ID = p.getId();
						break;
					}
					case Protocol.PrtLogout: {
						if (socket != null) {
							socket.close();
						}
						if (in != null) {
							in.close();
						}
						if (out != null) {
							out.close();
						}
						exitflag = true;
						return;
					}
					}
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
							} finally {
				receiver = null;
			}
		}
	}
	
	public boolean networkinit(String SIP, int SPort) {
		try {
			serverIP = SIP;
			serverPort = SPort;
			socket = new Socket(serverIP, serverPort);
			out = new ObjectOutputStream(socket.getOutputStream());
			in = new ObjectInputStream(socket.getInputStream());

			Protocol p = new Protocol();
			p.setCmd(Protocol.Prtlogin);

			out.writeObject(p);

			receiver = new Thread(new ReceiveMessage());
			receiver.start();
			return true;

		} catch (ConnectException ce) {
			ce.printStackTrace();
			socket = null;
			out = null;
			in = null;
		} catch (Exception e) {
			e.printStackTrace();
			socket = null;
			out = null;
			in = null;
		}
		return false;
	}

	public void logout() {
		try {
			Protocol p = new Protocol();
			p.setCmd(Protocol.PrtLogout);
			p.setId(ID);
			out.writeObject(p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setDeviceOn(int i){
		Protocol p = new Protocol();
		p.setCmd(Protocol.PrtSetItem);
		p.setId(ID);
		p.setCasenum(i);
		try {
			out.writeObject(p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendXml(){
		 try {
	            String fName = "test.Xml";
	            Protocol p = new Protocol();
	    		p.setCmd(Protocol.PrtFileSend);
	    		p.setId(ID);
	    		p.setFname(fName);
	            out.writeObject(p);
	 
	            File f = new File(fName);
	            FileInputStream fis = new FileInputStream(f);
	            BufferedInputStream bis = new BufferedInputStream(fis);
	 
	            int len;
	            int size = 4096;
	            byte[] data = new byte[size];
	            while ((len = bis.read(data)) != -1) {
	            	out.write(data, 0, len);
	            }
	 
	            out.flush();
	            bis.close();
	            fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
